package com.Endurancer.manager.service.impl;

import com.Endurancer.common.utils.DateUtils;
import com.Endurancer.manager.domain.Comment;
import com.Endurancer.manager.mapper.CommentMapper;
import com.Endurancer.manager.service.ICommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * 评论Service业务层处理
 *
 * @author Endurancer
 * @date 2025-02-04
 */
@Service
public class CommentServiceImpl implements ICommentService
{
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 查询评论
     *
     * @param commentId 评论主键
     * @return 评论
     */
    @Override
    public Comment selectCommentByCommentId(Long commentId)
    {
        return commentMapper.selectCommentByCommentId(commentId);
    }

    /**
     * 查询评论列表
     *
     * @param comment 评论
     * @return 评论
     */
    @Override
    public List<Comment> selectCommentList(Comment comment)
    {
        return commentMapper.selectCommentList(comment);
    }

    /**
     * 新增评论
     *
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment)
    {
        comment.setCreateTime(DateUtils.getNowDate());
        return commentMapper.insertComment(comment);
    }

    /**
     * 修改评论
     *
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment)
    {
        comment.setUpdateTime(DateUtils.getNowDate());
        return commentMapper.updateComment(comment);
    }

    /**
     * 批量删除评论
     *
     * @param commentIds 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByCommentIds(Long[] commentIds)
    {
        return commentMapper.deleteCommentByCommentIds(commentIds);
    }

    /**
     * 删除评论信息
     *
     * @param commentId 评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByCommentId(Long commentId)
    {
        return commentMapper.deleteCommentByCommentId(commentId);
    }

    @Override
    public PageInfo<Comment> selectTree(Long fid, String module, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> rootList = commentMapper.selectRoot(fid, module);
        PageInfo<Comment> pageInfo = PageInfo.of(rootList);

        for (Comment root : rootList) {
            Long rootId = root.getCommentId(); // 使用评论自己的ID作为rootId
            List<Comment> children = commentMapper.selectByRootId(rootId);
            // 对子评论进行排序，确保它们按照pid（父评论ID）、time（评论时间）和id（评论ID）排序
            children.sort(new Comparator<Comment>() {
                @Override
                public int compare(Comment c1, Comment c2) {
                    if (c1.getPid().equals(c2.getPid())) {
                        // 如果pid相同，则按时间排序
                        int timeCompare = c2.getTime().compareTo(c1.getTime());
                        if (timeCompare != 0) {
                            return timeCompare;
                        }
                        // 如果时间相同，则按id排序
                        return c1.getCommentId().compareTo(c2.getCommentId());
                    }
                    // 否则按pid排序
                    return c1.getPid().compareTo(c2.getPid());
                }
            });
            root.setChildren(children);
        }
        return pageInfo;
    }

    @Override
    public Integer selectCount(Long fid, String module) {
        return commentMapper.selectCount(fid,module);
    }

}
